var Sm = (function ($, sm, undefined) {
    sm.req = function (options) {
        var that = this
            , success = options.success
            , error = options.error
            , statusName = 'code'
            , msgName = 'message'
            , dataUrl = 'https://api.12301.co'//后台请求数据url
            , tokenName = 'token'
            , noAlert = false;

        options.data = options.data || {};
        options.headers = options.headers || {};
        if (options.noAlert != undefined) {
            noAlert = options.noAlert;
        }

        //自动给 Request Headers 传入 token
        // options.headers[tokenName] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJlMTExYjE2ZTgxODg0NWEwOWZkOTg5NTdjYWM2ZTczYyIsImV4cCI6MTU5OTcyMTE2N30.Iu8UGMcDaRsjMhsU6Lr8K2mEp6eMaNpZrP93Y9cYM-8';
        options.headers[tokenName] = tokenName in options.headers
            ? options.headers[tokenName]
            : (handleLocalStorage('get', tokenName) || '');
        options.headers['Access-Control-Allow-Origin'] = '*'
        options.url = (options.url.indexOf("http") > -1 || options.url.startsWith('json')) ? options.url : (dataUrl + options.url)
        options.async = options.async != null ? options.async : true;
        delete options.success;
        delete options.error;
        var layindex;
        return $.ajax($.extend({
            type: options.type || 'post'
            , dataType: 'json'
            , success: function (res, status, xhr) {
                if (res[statusName]) {
                    //只有 response 的 code 一切正常才执行 done
                    if (res[statusName] == 200) {
                        //返回的head中有token  则重新设置系统token
                        if (xhr.getResponseHeader('token')) {
                            handleLocalStorage('set', tokenName, xhr.getResponseHeader('token'))
                        }
                        typeof options.done === 'function' && options.done(res);
                    }

                    //登录状态失效，清除本地 access_token，并强制跳转到登入页
                    else if (res[statusName] == 201) {
                        handleLocalStorage('remove', tokenName)
                        //layer.open({
                        //content: '请登录'
                        //,btn: '我知道了'
                        //});
                        //跳转到登入页
                        handleLocalStorage('set', "url", window.location.href)
                        top.location.href = dataUrl + '/login/toWeixinLogin';
                    }

                    //其它异常
                    else {
                        if (noAlert === false) {
                            var errorText = [
                                '<cite>Error：</cite> ' + (res[msgName] || '系统错误')
                                //,debug()
                            ].join('');
                            layer.open({
                                content: errorText
                                , btn: '我知道了'
                            });
                        }

                    }
                } else {
                    typeof options.done === 'function' && options.done(res);
                }

                //只要 http 状态码正常，无论 response 的 code 是否正常都执行 success
                typeof success === 'function' && success(res);
            }
            , error: function (e, code) {
                var errorText = [
                    '请求异常，请重试<br><cite>错误信息：</cite>' + code
                    //,debug()
                ].join('');
                layer.open({
                    content: errorText
                    , btn: '我知道了'
                });


                typeof error === 'function' && error(res);
            }
            , beforeSend: function (e, code) {
                layindex = layer.open({type: 2});
            }
            , complete: function (e, code) {
                layer.close(layindex);
            }
        }, options));


    };
    return sm;

})(jQuery, window.sm = window.sm || {});

function handleLocalStorage(method, key, value) {
    switch (method) {
        case 'get' : {
            let temp = window.localStorage.getItem(key);
            if (temp) {
                return temp
            } else {
                return false
            }
        }
        case 'set' : {
            window.localStorage.setItem(key, value);
            break
        }
        case 'remove': {
            window.localStorage.removeItem(key);
            break
        }
        default : {
            return false
        }
    }
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return (false);
}

var fileUrl="http://file.12301.co/";